// pages/players/add-player/add-player.js
const { sexOptions } = require('../config')

Component({
  /**
   * 组件的初始数据
   */
  data: {
    sexOptions,
    operationText: '添加',
    playerNum: undefined,
    sex: '',
    nickname: '',
    age: '',
    remark: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    validate() {
      const { nickname } = this.data;
      let message;
      if (!nickname.trim()) {
        message = '请输入昵称';
      }
      if (message) {
        wx.showToast({
          title: message,
          icon: 'none',
        })
      }
      return !message;
    },
    getData() {
      const { nickname, sex, age, remark, playerNum } = this.data;
      return { nickname: nickname.trim(), sex, age, remark, playerNum }
    },
    handleCancel() {
      this.triggerEvent('cancel');
    },
    handleAdd() {
      if (this.validate()) {
        const data = this.getData();
        this.triggerEvent('change', data);
      }
    },
    clearFormData() {
      this.setData({
        operationText: '添加',
        nickname: '',
        sex: '',
        age: '',
        remark: '',
        playerNum: null,
      })
    },

    setEditPlayerInfo(player) {
      this.setData({ ...player, operationText: '编辑' })
    },
  }
})